#! /usr/bin/env node


// const program = require('commander');

// program
//     .version('1.0.0')
//     .parse(process.argv);

const program = require('commander');
const inquirer = require('inquirer');
const fs = require('fs');
const userList=require('./userList.json')
const promptList = [{
    type: 'input',
    message: '设置你的名字:',
    name: 'name',
    default: "test_user" // 默认值
},{
    type: 'input',
    message: '请输入手机号:',
    name: 'phone',
    // validate: function(val) {
    //     if(val.match(/\d{11}/g)) { // 校验位数
    //         return val;
    //     }
    //     return "请输入11位数字";
    // }
},{
    type: 'input',
    message: '请输入你的身份证号码:',
    name: 'prove'
}
];


program
    .version('1.0.0')
    .option('-a,--add','add something')
    .option('-u,--update','update something')
    .option('-r,--remove','remove somthing')
    .action(()=>{
        inquirer.prompt(promptList).then(answers => {
            // console.log(answers); // 返回的结果
           let obj= userList.find(item=>item.prove==answers.prove)
           if(!obj){
            // fs.appendFileSync(userList,JSON.stringify(answers))
            userList.push(answers)
            fs.writeFileSync('./userList.json',JSON.stringify(userList))
            // console.log(userList)
           }else{
                console.log("已经存在此用户")
           }
        })
    })
   
    program.parse(process.argv)

if(program.add) console.log(' add somthing')
if(program.update) console.log(' update something')
if(program.remove) console.log(' remove something')
// console.log(process.argv[2]+"你好")